package com.qf.joint.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.joint.pojo.Address;
import com.qf.joint.service.AddressService;
import com.qf.joint.utils.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Zhy
 * Date on 2020/9/12  22:39
 * @description
 */
@Controller
@RequestMapping("/address")
@CrossOrigin
public class AddressController {
    @Autowired
    private AddressService addressService;

    //前台查询所有一级二级地址
    @RequestMapping(value = "/show", method = RequestMethod.GET)
    @ResponseBody
    public List<Address> selectAll(){
        List<Address> list = addressService.selectAll();
        return list;
    }

    //前台 根据一级地址查询所有二级地址
    @RequestMapping(value = "/show2/{id}", method = RequestMethod.GET)
    @ResponseBody
    public List<Address> selectAll2(@PathVariable("id") Integer aid){
        List<Address> list = addressService.selectByAid(aid);
        return list;
    }

    //后台查询所有
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public Info<Address> selectAll2(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                    @RequestParam(name = "limit", required = true, defaultValue = "4") int limit){
        int count = addressService.selectAll().size();
        PageHelper.startPage(page,limit);
        List<Address> list = addressService.selectAll();
        PageInfo<Address> pageInfo = new PageInfo<>(list);
        Info<Address> info = new Info<>();
        if (null!=list && list.size()>0){
            info.setCode(0);
            info.setData(list);
            info.setCount(count);//数据库数据总数
            System.out.println("controller1-info="+info);
            return info;
        }
        info.setCode(1);
        info.setMsg("没有查询到数据");
        System.out.println("controller2-info="+info);
        return info;//-/WEB-INF/view/test.jsp
    }

    //后台新增
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView insertOne(ModelAndView mv,Address address){
        System.out.println("controller-address="+address);
        int res = addressService.insertOne(address);
        System.out.println("controller2-res="+res);
        //mv.addObject("objLi", list);
        //mv.setViewName("redirect:/address/findAll");
        //mv.setViewName("redirect:/admin/address");
        mv.setViewName("redirect:/admin/admin");//暂时就让去主界面
        return mv;
//        return "redirect:/findAll";//-/WEB-INF/view/test.jsp
    }

    //后台修改
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    @ResponseBody
    public Info<Address> updateOne(Address address) throws UnsupportedEncodingException {
        String name = new String(address.getName().getBytes("iso-8859-1"),"utf-8");
        address.setName(name);
        int res = addressService.updateOne(address);
        Info<Address> info = new Info<>();
        if(res==1){
            info.setCode(0);
        }
        else{
            info.setCode(1);
        }
        return info;
    }

    //后台删除
    @RequestMapping(value = "/delete/{aid}", method = RequestMethod.GET)
    @ResponseBody
    public Info<Address> deleteOne(ModelAndView mv,@PathVariable("aid") int aid){
        int res = addressService.deleteOne(aid);
        Info<Address> info = new Info<>();
        if(res==1){
            info.setCode(0);
        }
        else{
            info.setCode(1);
        }
        return info;
    }
}
